# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render_to_response
from igs.libs.hydrogeo.vwellpr import VWellPR
from igs.libs.hydrogeo.vwellnopr import VWellNoPR
from django.utils import simplejson as json
from igs.utils.convert import conv
from forms import *

def gc(request):
    formacpc = GC_AcPC()
    formacpsc = GC_AcPSC()
    formacpl = GC_AcPL()
    formacnopc = GC_AcNoPC()
    formacnopsc = GC_AcNoPSC()
    formacnopl = GC_AcNoPL()
    return render_to_response('gc/ecuaciones.html', {'titulo': "Ecuaciones", 'formacpc': formacpc, 'formacpsc': formacpsc, 'formacpl': formacpl , 'formacnopc': formacnopc, 'formacnopsc': formacnopsc, 'formacnopl': formacnopl})
    

def calcPozoPermAcC(request):
    
    form = GC_AcPC(request.GET)
    if form.is_valid():
        data = {}
        res = {}
        for k, v in request.GET.iteritems():
            if v == "":
                miss = k
            elif len(k) < 3:
                data[k] = float(v)    
        tu = request.GET['tun']
        qu = request.GET['qun']
        if miss == "s":
            res['s'] = VWellPR().dropdown_confined_aquifer(conv(data['Q'], qu, tu) , data['T'], data['r'], data['R'])
        elif miss == "Q":
            res['Q'] = VWellPR().flow_rate_confined_aquifer(conv(data['T'], tu, qu) , data['s'], data['r'], data['R'])
        elif miss == "T":
            res['T'] = VWellPR().transmisivity_confined_aquifer(conv(data['Q'], qu, tu), data['s'], data['r'], data['R'])
        elif miss == "R":
            res['R'] = VWellPR().transmisivity_confined_aquifer(conv(data['Q'], qu, tu), data['s'], data['r'], data['R'])
        elif miss == "r":
            res['r'] = VWellPR().observation_distance_confined(conv(data['Q'], qu, tu), data['T'], data['s'], data['R'])
        return HttpResponse(json.dumps(res), mimetype='application/json') 
    else:
        err = {}
        for k,v in form.errors.items():
            err[k] = unicode(v) 
        return HttpResponse(json.dumps(err), mimetype='application/json') 
    
def calcPozoPermAcSC(request):
    
    form = GC_AcPSC(request.GET)
    if form.is_valid():
        data = {}
        res = {}
        miss = ""
        for k, v in request.GET.iteritems():
            if ((v == "") and (k != "bp") and (k != "kp")):
                miss = k
            elif len(k) < 3:
                num = v
                if (num == ""):
                    num = "0"
                data[k] = float(num)
        tu = request.GET['tun']
        qu = request.GET['qun']
        ku = request.GET['kpun']
        if miss == "s":
            res['s'] = VWellPR().dropdown_semiconfined_aquifer(data['rp'], data['r'], conv(data['Q'], qu, tu), data['T'], data['B'] )
        elif miss == "Q":
            res['Q'] = VWellPR().flow_rate_semiconfined_aquifer(data['rp'], data['r'], conv(data['T'], tu, qu), data['s'], data['B'])
        elif miss == "T":
            res['T'] = VWellPR().transmisivity_semiconfined_aquifer(data['rp'], data['r'], conv(data['Q'], qu, tu), data['s'], conv(data['kp'],ku, tu), data['bp'])
        elif miss == "rp":
            res['rp'] = VWellPR().transmisivity_semi_confined_aquifer(conv(data['Q'], qu, tu), data['s'], data['r'], data['R'])
        elif miss == "r":
            res['r'] = VWellPR().observation_distance_semiconfined(data['rp'], data['s'], conv(data['Q'], qu, tu), data['T'], data['B'] )
        elif miss == "B":
            res['B'] = VWellPR().transmisivity_semi_confined_aquifer(conv(data['Q'], qu, tu), data['s'], data['r'], data['R'])
        return HttpResponse(json.dumps(res), mimetype='application/json') 
    else:
        err = {}
        for k,v in form.errors.items():
            err[k] = unicode(v) 
        return HttpResponse(json.dumps(err), mimetype='application/json') 

def calcPozoPermAcL(request):
    form = GC_AcPL(request.GET)
    if form.is_valid():
        data = {}
        res = {}
        for k, v in request.GET.iteritems():
            if v == "":
                miss = k
            elif len(k) < 3:
                data[k] = float(v)
        ku = request.GET['kun']
        qu = request.GET['qun']
        if miss == "s":
            res['s'] = VWellPR().dropdown_unconfined_aquifer(data['h'], conv(data['Q'], qu, ku), data['k'], data['ro'], data['R'])
        elif miss == "Q":
            res['Q'] = VWellPR().flow_rate_unconfined_aquifer(data['h'], conv(data['k'], ku, qu), data['s'], data['ro'], data['R'])
        elif miss == "k":
            res['k'] = VWellPR().hydraulic_conductivity_unconfined_aquifer(data['h'], conv(data['Q'], qu, ku), data['s'], data['ro'], data['R'])
        elif miss == "R":
            res['R'] = VWellPR().transmisivity_confined_aquifer(conv(data['Q'], qu, ku), data['s'], data['r'], data['R'])
        elif miss == "ro":
            res['ro'] = VWellPR().observation_distance_unconfined(data['h'], conv(data['Q'], qu, ku), data['k'], data['s'], data['R'])
        elif miss == "h":
            res['h'] = VWellPR().transmisivity_confined_aquifer(conv(data['Q'], qu, ku), data['s'], data['r'], data['R'])
        return HttpResponse(json.dumps(res), mimetype='application/json') 
    else:
        err = {}
        for k,v in form.errors.items():
            err[k] = unicode(v) 
        return HttpResponse(json.dumps(err), mimetype='application/json') 

# Pozos No permanentes!
def calcPozoNoPermAcC(request):
    
    form = GC_AcNoPC(request.GET)
    if form.is_valid():
        data = {}
        res = {}
        for k, v in request.GET.iteritems():
            if v == "":
                miss = k
            elif len(k) < 3:
                data[k] = float(v)    
        tu = request.GET['tun']
        qu = request.GET['qun']
        if miss == "s":
            res['s'] = VWellNoPR().dropdown_confined_aquifer(data['t'], conv(data['Q'], qu, tu), data['r'], data['T'], data['S'])
        elif miss == "Q":
            res['Q'] = VWellNoPR().flow_rate_confined_aquifer(data['t'], data['s'], data['r'], conv(data['T'], tu, qu) , data['S'])
        elif miss == "T":
            res['T'] = VWellNoPR().transmisivity_confined_aquifer(conv(data['Q'], qu, tu), data['s'], data['r'], data['R'])
        elif miss == "R":
            res['R'] = VWellNoPR().transmisivity_confined_aquifer(conv(data['Q'], qu, tu), data['s'], data['r'], data['R'])
        elif miss == "r":
            res['r'] = VWellNoPR().observation_distance_confined(conv(data['Q'], qu, tu), data['T'], data['s'], data['R'])
        return HttpResponse(json.dumps(res), mimetype='application/json') 
    else:
        err = {}
        for k,v in form.errors.items():
            err[k] = unicode(v) 
        return HttpResponse(json.dumps(err), mimetype='application/json') 

def calcPozoNoPermAcSC(request):
    
    form = GC_AcNoPSC(request.GET)
    if form.is_valid():
        data = {}
        res = {}
        for k, v in request.GET.iteritems():
            if v == "":
                miss = k
            elif len(k) < 3:
                data[k] = float(v)    
        tu = request.GET['tun']
        qu = request.GET['qun']
        if miss == "s":
            res['s'] = VWellNoPR().dropdown_semiconfined_aquifer(data['r'], data['t'], conv(data['Q'], qu, tu), data['T'], data['S'], data['B'], data['kp'], data['bp'])
        elif miss == "Q":
            res['Q'] = VWellNoPR().flow_rate_semiconfined_aquifer(data['r'], data['t'], data['s'],  conv(data['T'], tu, qu) , data['S'], data['B'], data['kp'], data['bp'])
        elif miss == "T":
            res['T'] = VWellNoPR().transmisivity_confined_aquifer(conv(data['Q'], qu, tu), data['s'], data['r'], data['R'])
        elif miss == "R":
            res['R'] = VWellNoPR().transmisivity_confined_aquifer(conv(data['Q'], qu, tu), data['s'], data['r'], data['R'])
        elif miss == "r":
            res['r'] = VWellNoPR().observation_distance_confined(conv(data['Q'], qu, tu), data['T'], data['s'], data['R'])
        return HttpResponse(json.dumps(res), mimetype='application/json') 
    else:
        err = {}
        for k,v in form.errors.items():
            err[k] = unicode(v) 
        return HttpResponse(json.dumps(err), mimetype='application/json') 

def calcPozoNoPermAcL(request):
    form = GC_AcNoPL(request.GET)
    if form.is_valid():
        data = {}
        res = {}
        for k, v in request.GET.iteritems():
            if v == "":
                miss = k
            elif len(k) < 3:
                data[k] = float(v)
        ku = request.GET['kun']
        qu = request.GET['qun']
        if miss == "s":
            res['s'] = VWellNoPR().dropdown_unconfined_aquifer(data['t'], conv(data['Q'], qu, ku), data['r'], data['k'], data['S'], data['h'])
        elif miss == "Q":
            res['Q'] = VWellNoPR().flow_rate_unconfined_aquifer(data['t'], data['s'], data['r'], conv(data['k'], ku, qu), data['S'], data['h'])
        elif miss == "k":
            res['k'] = VWellNoPR().hydraulic_conductivity_unconfined_aquifer(data['h'], conv(data['Q'], qu, ku), data['s'], data['ro'], data['R'])
        elif miss == "R":
            res['R'] = VWellNoPR().transmisivity_confined_aquifer(conv(data['Q'], qu, ku), data['s'], data['r'], data['R'])
        elif miss == "ro":
            res['ro'] = VWellNoPR().observation_distance_unconfined(data['h'], conv(data['Q'], qu, ku), data['k'], data['s'], data['R'])
        elif miss == "h":
            res['h'] = VWellNoPR().transmisivity_confined_aquifer(conv(data['Q'], qu, ku), data['s'], data['r'], data['R'])
        return HttpResponse(json.dumps(res), mimetype='application/json') 
    else:
        err = {}
        for k,v in form.errors.items():
            err[k] = unicode(v) 
        return HttpResponse(json.dumps(err), mimetype='application/json') 
